home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch19 / Distance.bas next >
BASIC Source File  |  1999-07-11  |  3KB  |  86 lines

  1. Attribute VB_Name = "Distances"
  2. ' Three-dimensional distance functions.
  3.  
  4. Option Explicit
  5.  
  6.  
  7.  
  8. ' Return the distance between two points.
  9. Public Function DistancePointToPoint(ByVal x1 As Single, ByVal y1 As Single, ByVal z1 As Single, ByVal x2 As Single, ByVal y2 As Single, ByVal z2 As Single) As Single
  10. Dim dx As Single
  11. Dim dy As Single
  12. Dim dz As Single
  13.  
  14.     dx = x2 - x1
  15.     dy = y2 - y1
  16.     dz = z2 - z1
  17.     DistancePointToPoint = Sqr(dx * dx + dy * dy + dz * dz)
  18. End Function
  19. ' Return the distance between a point and a line.
  20. Public Function DistancePointToLine(ByVal x1 As Single, ByVal y1 As Single, ByVal z1 As Single, ByVal x2 As Single, ByVal y2 As Single, ByVal z2 As Single, ByVal vx2 As Single, ByVal vy2 As Single, ByVal vz2 As Single) As Single
  21. Dim ax As Single
  22. Dim ay As Single
  23. Dim az As Single
  24. Dim len_a_squared As Single
  25. Dim a_dot_v As Single
  26. Dim len_v As Single
  27. Dim a_dot_v_over_len_v As Single
  28.  
  29.     ax = x2 - x1
  30.     ay = y2 - y1
  31.     az = z2 - z1
  32.  
  33.     len_a_squared = ax * ax + ay * ay + az * az
  34.     a_dot_v = ax * vx2 + ay * vy2 + az * vz2
  35.     len_v = Sqr(vx2 * vx2 + vy2 * vy2 + vz2 * vz2)
  36.     a_dot_v_over_len_v = a_dot_v / len_v
  37.  
  38.     DistancePointToLine = Sqr( _
  39.         len_a_squared - a_dot_v_over_len_v * a_dot_v_over_len_v)
  40. End Function
  41. ' Return the distance between a point and a plane.
  42. Public Function DistancePointToPlane(ByVal x1 As Single, ByVal y1 As Single, ByVal z1 As Single, ByVal x2 As Single, ByVal y2 As Single, ByVal z2 As Single, ByVal nx2 As Single, ByVal ny2 As Single, ByVal nz2 As Single) As Single
  43. Dim ax As Single
  44. Dim ay As Single
  45. Dim az As Single
  46. Dim a_dot_n As Single
  47. Dim len_n As Single
  48.  
  49.     ax = x2 - x1
  50.     ay = y2 - y1
  51.     az = z2 - z1
  52.  
  53.     a_dot_n = ax * nx2 + ay * ny2 + az * nz2
  54.     len_n = Sqr(nx2 * nx2 + ny2 * ny2 + nz2 * nz2)
  55.  
  56.     DistancePointToPlane = Abs(a_dot_n / len_n)
  57. End Function
  58. ' Return the distance between two lines.
  59. Public Function DistanceLineToLine(ByVal x1 As Single, ByVal y1 As Single, ByVal z1 As Single, ByVal x2 As Single, ByVal y2 As Single, ByVal z2 As Single, ByVal vx1 As Single, ByVal vy1 As Single, ByVal vz1 As Single, ByVal vx2 As Single, ByVal vy2 As Single, ByVal vz2 As Single) As Single
  60. Dim len_a As Single
  61. Dim len_b As Single
  62. Dim a_dot_b As Single
  63. Dim nx As Single
  64. Dim ny As Single
  65. Dim nz As Single
  66.  
  67.     ' See if the lines are parallel.
  68.     len_a = Sqr(vx1 * vx1 + vy1 * vy1 + vz1 * vz1)
  69.     len_b = Sqr(vx2 * vx2 + vy2 * vy2 + vz2 * vz2)
  70.     a_dot_b = vx1 * vx2 + vy1 * vy2 + vz1 * vz2
  71.     If a_dot_b = len_a * len_b Then
  72.         ' The lines are parallel.
  73.         DistanceLineToLine = _
  74.             DistancePointToLine(x1, y1, z1, _
  75.                 x2, y2, z2, vx2, vy2, vz2)
  76.     Else
  77.         ' The lines are not parallel.
  78.         ' Get the normal to both vectors.
  79.         m3Cross nx, ny, nz, vx1, vy1, vz1, vx2, vy2, vz2
  80.  
  81.         DistanceLineToLine = _
  82.             DistancePointToPlane(x1, y1, z1, _
  83.                 x2, y2, z2, nx, ny, nz)
  84.     End If
  85. End Function
  86.